<?php
/**
 * PHP Daycare
 * This file will handle checking a child out, including all checks
 * @author Jason Butz
 * @version 0.0
 * @package PHPDaycare
 */
include_once("functions.php");
include_once("config.php");
// The user MUST be logged in to view this page!
session_start();
if(!$session->isloggedin())
{
	echo "<p>You must be logged in to view this page. Please <a href=\"index.php\">login</a> to continue.</p>";
}
else // They are logged in!
{
	if(!isset($_GET['cid']))
	{
		// We need to get a list of children from the database
		$r = $db->query("SELECT ID, name FROM `child` WHERE (ID) IN (SELECT attendance.child_id As `ID` FROM attendance WHERE attendance.out IS NULL);");
		echo "
		<p>Please select the child you wish to check out</p>
		<table cellspacing='1' class='tablesorter' width='100%'>
		<thead>
			<tr>
				<th class=\"{sorter: 'text'}\">Name</th>
				<th class=\"{sorter: 'text'}\">Options</th>
			</tr>
		</thead>
		<tbody>";
		while($row = mysql_fetch_row($r))
		{
			echo "<tr>
						<td>" . $row[1] . "</td>
						<td><a href= \"#\" onClick=\"loadinfo('checkout.php', 'cid=".$row[0]."')\">Check-Out</a></td>
					</tr>";
		}
		echo "</tbody></table>";
		$temp->tablePager();
		echo "<script type=\"text/javascript\">loadsorter()</script>";
	}
	else if(isset($_GET['checkout']))
	{
		// We need to check-out this child
		// Do we have have all the info?
		$allSet = true;
		if(!isset($_GET['adult']))
		{
			echo "<p>Error: No adult was set! This could be a hacking attempt.</p>";
			$allSet = false;
		}
		if(!isset($_GET['cid']))
		{
			echo "<p>Error: No child id was set! This could be a hacking attempt.</p>";
			$allSet = false;
		}
		if($allSet)
		{
			$r2 = $db->query("UPDATE attendance SET parent_out = ".$_GET['adult']." , `out` = CURRENT_TIMESTAMP WHERE child_id = ".$_GET['cid'].";");
			if(!$r2)
			{
				echo "<p>An error occurred, the child was not checked out.</p>";
			}
			else
			{
				$log->checkout($_GET['cid'], $_GET['adult'], $_SESSION['user']);
				echo "<p>The operation completed successfully. The child was checked out.</p>";
			}
		}
	}
	else
	{
		$cid = $_GET['cid'];
		// A child has been selected. We must verify the information
		// Does this child exist?
		$r = $db->query("SELECT ID, name FROM child WHERE ID=\"$cid\";");
		if(!mysql_fetch_row($r))
		{
			// No such child exists!
			echo "<p>Error: No such child exists!</p>";
		}
		else
		{
			// And we need to know who is checking them in
			$child = $db->query("SELECT ID, name, notes FROM child WHERE ID=$cid;");
			$parent = $db->query("SELECT adults.ID, adults.name FROM `c_a_rel` JOIN `adults` ON c_a_rel.adults = adults.ID WHERE child = $cid");
			//Process DB stuff
			while($row = mysql_fetch_row($parent))
			{
				$id = $row[0];
				$name = $row[1];
				$adults .= "<label><input type='radio' name='adult' value='$id' onClick='AdultVal=$id' /><a class='button' href='#' onClick=\"placePhoto(".$id.", 'a');placeTitle('".$name."');\">".$name."</a></label><br />";
			}
			$child = mysql_fetch_row($child);
			//
			//Output HTML
			echo "<h2>Currently checking out: ".$child[1]." </h2>
				<p>Please select the adult checking this child out. No one other than the authorized adults may checkout the child.</p>
				<input type='button' class='button' value='View Childs Photo' onClick=\"placePhoto(".$child[0].", 'c');placeTitle('".$child[1]."');\">
				<form name='checkout'>
				  <p>
					 ".$adults."
				  </p>
				  <p>
					 <input type='button' tabindex='4' value='Complete Checkout' onClick=\"loadinfo('checkout.php', 'cid=".$cid."&checkout=1&adult=' + AdultVal)\"/>
				  </p>
				</form>";
			$temp->jqPopup("", "");
			echo "<script type='text/javascript'>loadPopupCalls()</script>";
		}
	}
}
